import { Injectable } from '@nestjs/common';
import { ReturnModelType } from '@typegoose/typegoose';
import { Workspace } from 'libs/db/src/model/workspace.model';
import { InjectModel } from 'nestjs-typegoose';
import { TabCollectionDetailDto } from 'src/tab-collection/dto/TabCollectionDetail.dto';
import { TabCollectionService } from 'src/tab-collection/tab-collection.service';
import { CreateWorkspaceDto } from './dto/createWorkspace.dto';
import { WorkspaceDetailDto } from './dto/workspaceDetail.dto';

@Injectable()
export class WorkspaceService {
  constructor(
    @InjectModel(Workspace)
    private readonly workSpaceModel: ReturnModelType<typeof Workspace>,
    private readonly collectionService: TabCollectionService,
  ) {}

  // 创建workspace
  async create(workspace: CreateWorkspaceDto, uid: string) {
    //创建一个workspace并且在workSpace下去创建一个
    const newWorkSpace = await this.workSpaceModel.create({
      name: workspace.name,
      uid,
    });
    await this.collectionService.createEmptyCustomed(newWorkSpace.id);
  }

  //修改workspace的名字
  async modifyName(workspaceInfo: CreateWorkspaceDto, wid: string) {
    await this.workSpaceModel.findByIdAndUpdate(wid, workspaceInfo);
  }

  //查询是否存在workspace的uid和name都相同的workspace
  async hasEqual(workspace: CreateWorkspaceDto, uid: string) {
    const result = await this.workSpaceModel.findOne({
      name: workspace.name,
      uid: uid,
    });

    if (result) {
      return true;
    }

    return false;
  }

  // getWorkspaceByID
  async getWorkspaceByUid(uid: string): Promise<Workspace[] | undefined> {
    // 查找ID
    return this.workSpaceModel.find({ uid: uid });
  }

  //删除用户的workspace
  async deleteWorkspace(id: string) {
    //删除指定的workspace
    const collections = await this.collectionService.findAllCollectionByWid(id);
    for (const collection of collections) {
      await this.collectionService.remove(collection.id);
    }
    //删除指定的workspace
    await this.workSpaceModel.deleteOne({
      _id: id,
    });
  }

  async getWorkspaceDetail(wid: string): Promise<WorkspaceDetailDto> {
    //先去找到对应的workspace
    const workspace = await this.findOne(wid);
    const tabsInfo = await this.getTabsInfoByIsCustomed(wid, false);
    const customedTabsInfo = await this.getTabsInfoByIsCustomed(wid, true);
    return {
      id: wid,
      name: workspace.name,
      createTime: workspace.createdAt.getTime(),
      tabsInfo,
      customedTabsInfo,
    };
  }

  async getTabsInfoByIsCustomed(
    wid: string,
    isCustomed: boolean,
  ): Promise<TabCollectionDetailDto[]> {
    const collections = await this.collectionService.findCollectionByIsCustomed(
      wid,
      isCustomed,
    );
    const tabsInfo = [];
    for (const collection of collections) {
      const detail = await this.collectionService.findDetail(collection.id);
      tabsInfo.push(detail);
    }
    return tabsInfo;
  }

  async findOne(wid: string): Promise<Workspace | undefined> {
    return await this.workSpaceModel.findOne({ _id: wid });
  }
}
